from fastapi import APIRouter, Query

from app.core.handlers.response import BaseResponse, FailResponse, SuccessResponse
from app.puzzle.schema import _IE, FakeCoinResultSchema
from app.puzzle.service import (
    fake_coin_puzzle_get_method,
    fake_coin_puzzle_get_result,
)

fake_coin_router = APIRouter(
    prefix="/fake_coin_puzzle",
    tags=["Fake Coin Puzzle"],
    responses={404: {"description": "Not found"}},
)


@fake_coin_router.get(
    "/get_method",
    summary="获取硬币的称量法",
    response_model=BaseResponse[list[list[_IE]]],
    description="""
    ## 假币问题 - 获取解法
    给定硬币数量，返回一个解法，用于判断是否有一枚假币，以及哪一枚是假币
    """,
)
async def fake_coin_puzzle_get_method_route(
    n: int = Query(..., ge=3, description="硬币数量"),
):
    ok, result = fake_coin_puzzle_get_method(n)
    if ok:
        return SuccessResponse(**result)
    else:
        return FailResponse(**result)


@fake_coin_router.post(
    "/get_result",
    summary="获取硬币的称量结果",
    response_model=BaseResponse[int],
    description="""
    ## 假币问题 - 获取结果
    给定硬币数量和称量结果，返回一个假币结果
    """,
)
async def fake_coin_puzzle_get_result_route(
    data: FakeCoinResultSchema,
):
    matrix = data.matrix
    result_list = data.result_list
    ok, result = fake_coin_puzzle_get_result(matrix, result_list)
    if ok:
        return SuccessResponse(
            message=str(result.get("message")), data=result.get("data")
        )
    else:
        return FailResponse(message=str(result.get("message")))
